local wen__suhen = fk.CreateSkill{
  name = "wen__suhen",
}

Fk:loadTranslationTable{
  ["wen__suhen"] = "溯痕",
  [":wen__suhen"] = "出牌阶段限一次，你可以失去1点体力，然后本阶段你使用下一张牌后，将手牌数调整至你使用此牌前。",

  ["#wen__suhen"] = "溯痕：你可以失去1点体力，然后本阶段你使用下一张牌后，将手牌数调整至你使用此牌前",
  ["@@suhen"] = "溯痕",
}

wen__suhen:addEffect("active", {
  prompt = "#wen__suhen",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(wen__suhen.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:loseHp(player, 1, wen__suhen.name)
    room:setPlayerMark(player, "@@suhen", 1)
  end,
})

wen__suhen:addEffect(fk.PreCardUse, {
  can_trigger = function(self, event, target, player, data)
    return target == player
    and player:getMark("@@suhen") == 1
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "suhen", player:getHandcardNum())
  end,
})

wen__suhen:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    return target == player
    and player:isAlive()
    and player:getMark("@@suhen") == 1
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local hand_diff = player:getHandcardNum() - player:getMark("suhen")

    if hand_diff > 0 then
      room:askToDiscard(player, {
        min_num = hand_diff,
        max_num = hand_diff,
        include_equip = false,
        skill_name = wen__suhen.name,
        cancelable = false,
      })
    elseif hand_diff < 0 then
      local draw_num = -hand_diff
      player:drawCards(draw_num, wen__suhen.name)
    end

    room:setPlayerMark(player, "@@suhen", 0)
    room:setPlayerMark(player, "suhen", 0)
  end,
})

wen__suhen:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(wen__suhen.name)
      and data.phase == Player.Play
      and player:getMark("@@suhen") ~= 0
  end,

  on_cost = Util.TrueFunc,

  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@suhen", 0)
    player.room:setPlayerMark(player, "suhen", 0)
  end,
})

return wen__suhen